package easyaccept.web.util.bean.formatter;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import easyaccept.web.util.bean.BeanHelperUtil;
import easyaccept.web.util.bean.message.MessageResources;

 
public class Formatter {
	
	public static String DEFAULT_DATE_FORMAT = "dd/MM/yyyy";	
	public static String DEFAULT_TIMESTAMP_FORMAT = "dd/MM/yyyy HH:mm";	
	public static String DEFAULT_TIME_FORMAT = "HH:mm";
	
	/**
	 * Gets a pattern from resource
	 * @param key
	 * @return
	 */
	public static String getResourcePattern(String key) {		
		 MessageResources resources = BeanHelperUtil.getMessageResources();
	    if ((resources != null) && resources.isPresent(key)) {
            return (String) resources.getMessage(key);
        }
	   return null;
	}

	/**
	 *  Format Date
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(Date date, String pattern) {
		SimpleDateFormat formatter = getDateFormatter(pattern);
		return formatter.format(date);
	}	
	
	/**
	 * Parse Date
	 * @param value
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDate(String value, String pattern) throws ParseException {
		SimpleDateFormat formatter = getDateFormatter(pattern);
		return formatter.parse(value);
	}
	
	/**
     * Format Date by resource pattern
     * @param value
     * @return
     */
	public static String format(Date date) {
		String pattern = getResourcePattern(Date.class.getName());
		SimpleDateFormat formatter = getDateFormatter(pattern);
		return formatter.format(date);
	}	
	
	 /**
     * Parse Date by resource pattern
     * @param value
     * @return
     * @throws ParseException
     */
	public static Date parseDate(String value) throws ParseException {
		String pattern = getResourcePattern(Date.class.getName());
		SimpleDateFormat formatter = getDateFormatter(pattern);
		return formatter.parse(value);
	}
	

	/**
	 * Date formatter
	 */
	public static SimpleDateFormat getDateFormatter(String pattern) {		
		if (pattern == null) {
			pattern = DEFAULT_DATE_FORMAT;
		}
		Locale locale = BeanHelperUtil.getLocale();
		SimpleDateFormat format = (locale != null) ? new SimpleDateFormat(
				pattern, locale) : new SimpleDateFormat(pattern);

		return format;
	}	
	
	
	//Decimal
	
	/**
	 * Decimal formatter
	 */
    public static DecimalFormat getDecimalFormatter() {
        Locale locale = BeanHelperUtil.getLocale();
        locale = ((locale != null) ? locale : Locale.getDefault());

        DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(locale);

        formatter.setMinimumFractionDigits(2);
        formatter.setGroupingSize(3);

        return formatter;
    }
    
    /**
     * Format Decimal
     * @param value
     * @return
     */
    public static String format(Number value) {
        DecimalFormat formatter =  Formatter.getDecimalFormatter();
        String str = formatter.format(value);
        return str;
    }
    
    
    /**
     * Parse Decimal
     * @param value
     * @return
     * @throws ParseException
     */
     public static Number parseDecimal(String value) throws ParseException {
         DecimalFormat formatter = Formatter.getDecimalFormatter();
         Number n = formatter.parse(value);
         return n;
     }
     
     


}
